package com.flushbonading.controller;


import com.auth0.jwt.JWT;
import com.flushbonading.annotation.BackStage;
import com.flushbonading.annotation.UserLoginToken;
import com.flushbonading.pojo.Menu;
import com.flushbonading.pojo.User;
import com.flushbonading.pojo.dto.MenuDTO;
import com.flushbonading.service.IMenuService;
import com.flushbonading.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zlx
 * @since 2021-04-07
 */
@RestController
@RequestMapping("/menu")
@Api(value = "菜单模块" ,tags = "菜单接口")
@BackStage
public class MenuController {
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IUserService userService;
    @UserLoginToken
    @ApiOperation(value = "传入用户id获得菜单")
    @PostMapping("/all")
    @Cacheable("menu")
    public List<Menu> getMenu(HttpServletRequest request){
        String token = request.getHeader("token");
        String user_id = JWT.decode(token).getAudience().get(0);
        User role=userService.getRoleByUserId(user_id);
        List<Menu> menus=menuService.getRoleMenu(role.getRole_id());
        handleMenus(menus);
        return menus;
    }

    public void handleMenus(List<Menu> menus) {
        for (Menu menu : menus) {
            List<Menu> children = getMenus(menu.getMenu_id(),menus);
            if(children.size()>0){
                menu.setChildren(children);
            }

        }

        Iterator<Menu> iterator = menus.iterator();
        while (iterator.hasNext()) {
            Menu menu = iterator.next();
            if (menu.getParent_id() != 0) {
                iterator.remove();
            }
        }
    }

    private List<Menu> getMenus(Integer menu_id, List<Menu> menus) {
        List<Menu> children =new ArrayList<>() ;
        for (Menu menu : menus) {
            if(menu.getParent_id()==menu_id){
                children.add(menu);
            }
        }
        return  children;
    }

    @PostMapping("/permission/{role_id}")
    public List<MenuDTO> getPermission(@PathVariable("role_id") int role_id){
        List<MenuDTO> menuDTOS = menuService.getAllPermission();
        List<MenuDTO> menuDTOS1 = menuService.selectMenuByRoleID(role_id);
        for (MenuDTO menus:menuDTOS) {
            if (menuDTOS1.contains(menus))
                menus.setChecked(true);
        }
        menuDTOS.forEach(System.out::println);
        handleMenusDTO(menuDTOS);
        for (MenuDTO menus: menuDTOS) {
            List<MenuDTO> children = menus.getChildren();
            if (children!=null){
                menus.setChecked(false);
                for (MenuDTO ch: children){
                    if (ch.getChildren()!=null)
                        ch.setChecked(false);
                }
            }
        }
        System.out.println();
        menuDTOS.forEach(System.out::println);
        return menuDTOS;
    }

    public void handleMenusDTO(List<MenuDTO> menus) {
        for (MenuDTO menu : menus) {
            List<MenuDTO> children = getMenusDTO(menu.getId(),menus);
            if(children.size()>0){
                menu.setChildren(children);
            }

        }

        Iterator<MenuDTO> iterator = menus.iterator();
        while (iterator.hasNext()) {
            MenuDTO menu = iterator.next();
            if (menu.getParent_id() != 0) {
                iterator.remove();
            }
        }
    }

    private List<MenuDTO> getMenusDTO(Integer menu_id, List<MenuDTO> menus) {
        List<MenuDTO> children =new ArrayList<>() ;
        for (MenuDTO menu : menus) {
            if(menu.getParent_id()==menu_id){
                children.add(menu);
            }
        }
        return  children;
    }
}
